function box_initkernel() {
    if(filter.type == 0) {
	filter.kernel = null;
	return;
    }
    filter.kernel = new Array();
    for(var i = 0; i < 3; ++i) {
	filter.kernel[i] = new Array();
	for(var j = 0; j < 3; ++j) {
	    filter.kernel[i][j] = 0;
	}
    }
    filter.kernel[1][1] = 1;

    if(filter.type == 1) {
	//Smooth
	
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = 1;
	    }
	}
	filter.kernel[1][1] = 3;
	
		
	/*
	// raised
	filter.kernel[1][1] = 2;
	filter.kernel[2][0] = -2;
	filter.kernel[0][2] = 1;
	*/
	
	/*
	//Edge detection
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = -1;
	    }
	}
	filter.kernel[1][1] = 8;
	*/
	
	/*
	// Smoothing 2
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = 1.0/12.0;
	    }
	}
	filter.kernel[1][1] = 1.0/3.0;
	*/
	
	/*
	// Anti aliasing
	filter.kernel[1][1] = 1.0/4.0;
	
	filter.kernel[0][1] = 1.0/8.0;
	filter.kernel[1][0] = 1.0/8.0;
	filter.kernel[1][2] = 1.0/8.0;
	filter.kernel[2][1] = 1.0/8.0;
	
	filter.kernel[0][0] = 1.0/16.0;
	filter.kernel[0][2] = 1.0/16.0;
	filter.kernel[2][0] = 1.0/16.0;
	filter.kernel[2][2] = 1.0/16.0;
	*/
	
	return;
    }
    
    if(filter.type == 2) {
	//Sharpen
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = -1;
	    }
	}
	filter.kernel[1][1] = 9;
	return;
    }
    
    if(filter.type == 3) {
	//Motion blur
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = 0;
	    }
	}
	filter.kernel[0][2] = 1;
	filter.kernel[2][0] = 1;
	filter.kernel[2][1] = 0.4;
	return;
    }
    
    if(filter.type == 4) {
	// raised
	filter.kernel[1][1] = 2;
	filter.kernel[2][0] = -2;
	filter.kernel[0][2] = 1;
	return;
    }
    
    if(filter.type == 5) {
	//Edge detection
	
	for(var i = 0; i < 3; ++i) {
	    for(var j = 0; j < 3; ++j) {
		filter.kernel[i][j] = -1;
	    }
	}
	filter.kernel[1][1] = 8;
	
	return;
    }
    
    if(filter.type == 6) {
	// Anti aliasing
	filter.kernel[1][1] = 1.0/4.0;
	
	filter.kernel[0][1] = 1.0/8.0;
	filter.kernel[1][0] = 1.0/8.0;
	filter.kernel[1][2] = 1.0/8.0;
	filter.kernel[2][1] = 1.0/8.0;
	
	filter.kernel[0][0] = 1.0/16.0;
	filter.kernel[0][2] = 1.0/16.0;
	filter.kernel[2][0] = 1.0/16.0;
	filter.kernel[2][2] = 1.0/16.0;
	return;
    }
}

function boxfiltering(flush) {
    if(filter.type == 0) return;

    loaddisp();
    box_initkernel();
    
    var wsum = 0;
    var pixels = new Array();
    for(var i = 0; i < display.xres; ++i) {
	pixels[i] = new Array();
	for(var j = 0; j < display.yres; ++j) {
	    pixels[i][j] = new rgbcolor(0,0,0,255);
	}
    }
    for(var i = 0; i < 3; ++i) {
	for(var j = 0; j < 3; ++j) {
	    wsum += filter.kernel[i][j];
	}
    }
    if(wsum == 0) wsum = 1;
    for(var i = 1; i < display.xres-1; ++i) {
	for(var j = 1; j < display.yres-1; ++j) {
	    var rgb = new rgbcolor(0,0,0,1);
	    var color = getdisp(i,j-1);
	    color.mul2(filter.kernel[1][0]);
	    rgb.add(color);
	    color = getdisp(i-1,j);
	    color.mul2(filter.kernel[0][1]);
	    rgb.add(color);
	    color = getdisp(i,j);
	    color.mul2(filter.kernel[1][1]);
	    rgb.add(color);
	    color = getdisp(i+1,j);
	    color.mul2(filter.kernel[2][1]);
	    rgb.add(color);
	    color = getdisp(i,j+1);
	    color.mul2(filter.kernel[1][2]);
	    rgb.add(color);
	    color = getdisp(i-1,j-1);
	    color.mul2(filter.kernel[0][0]);
	    rgb.add(color);
	    color = getdisp(i+1,j-1);
	    color.mul2(filter.kernel[2][0]);
	    rgb.add(color);
	    color = getdisp(i-1,j+1);
	    color.mul2(filter.kernel[0][2]);
	    rgb.add(color);
	    color = getdisp(i+1,j+1);
	    color.mul2(filter.kernel[2][2]);
	    rgb.add(color);

	    rgb.r = Math.floor(rgb.r/wsum);
	    rgb.g = Math.floor(rgb.g/wsum);
	    rgb.b = Math.floor(rgb.b/wsum);
	    if(rgb.r > 255) rgb.r = 255;
	    if(rgb.g > 255) rgb.g = 255;
	    if(rgb.b > 255) rgb.b = 255;

	    pixels[i][j].r = rgb.r;
	    pixels[i][j].g = rgb.g;
	    pixels[i][j].b = rgb.b;
	}
    }
    for(var i = 0; i < display.xres; ++i) {
	for(var j = 0; j < display.yres; ++j) {
	    putdisp(i,j,pixels[i][j]);
	}
    }
    if(flush)
	flushdisp();
}